package com.scau.ums.service.imp.suggest;

import com.scau.ums.mapper.AdminMapper;
import com.scau.ums.mapper.CustomSuggestionMapper;
import com.scau.ums.mapper.SuggestionMapper;
import com.scau.ums.model.*;
import com.scau.ums.service.LimitsSuggest;
import com.scau.ums.service.imp.info.DefaultSendInfoImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * author: 莫伟杰
 * date： 2019/7/23 14:52
 */
@Service
public class AdminSuggestImp implements LimitsSuggest {

    private AdminMapper adminMapper;
    private SuggestionMapper suggestionMapper;
    private CustomSuggestionMapper customSuggestionMapper;
    private DefaultSendInfoImp sendInfoImp;

    @Autowired
    public AdminSuggestImp(AdminMapper adminMapper, SuggestionMapper suggestionMapper,CustomSuggestionMapper customSuggestionMapper,DefaultSendInfoImp sendInfoImp) {
        this.adminMapper = adminMapper;
        this.suggestionMapper = suggestionMapper;
        this.customSuggestionMapper = customSuggestionMapper;
        this.sendInfoImp = sendInfoImp;
    }

    @Override
    public List<Suggestion> getAllSuggestions() {
        return suggestionMapper.selectByExample(null);
    }//获取所有言论

    @Override
    public List<Suggestion> getUserSuggestions(String userID) {
        SuggestionExample example = new SuggestionExample();
        example.createCriteria().andSpeakeridEqualTo(userID);
        return suggestionMapper.selectByExample(example);
    }

    //获取最新n条记录,startIndex=开始下标（从0开始,含下标）,pageSize=记录条数
    @Override
    public List<Suggestion> getLatestSuggestions(int startIndex, int pageSize) {
        String whereClause = "state = '"+STATE_PASS+"'";
        return customSuggestionMapper.selectRowByPageSetOrderByClause
                ("suggestion","date DESC",startIndex,pageSize,whereClause);
    }

    //获取最新n条记录,startIndex=开始下标（从0开始,含下标）,pageSize=记录条数
    //管理员可以获取所有类型的记录
    public List<Suggestion> getLatestAllSuggestions(int startIndex, int pageSize) {
        return customSuggestionMapper.selectByPageSetOrderByClause
                ("suggestion","date DESC",startIndex,pageSize);
    }

    public List<Suggestion> getUnverifySuggestions()
    {
        SuggestionExample example = new SuggestionExample();
        example.createCriteria().andStateEqualTo(STATE_VERIFY);
        return suggestionMapper.selectByExample(example);
    }

    public boolean verifySuggestion(Suggestion s, String state)
    {
        if(!s.getState().equals(STATE_VERIFY)) return false;
        if(!state.equals(STATE_PASS)&&!state.equals(STATE_NOTPASS)) return false;
        s.setState(state);
        if(suggestionMapper.updateByPrimaryKey(s)!=1) return false;
        String content = "您的建议“"+s.getContent()+"”审核"+state;
        Info info = sendInfoImp.createInfo("审核结果","system","高校后勤管理平台\n\n"+s.getSpeakerid(),s.getSpeakertype(),content);
        sendInfoImp.sendInfo(info);
        return true;
    }

    public void changeSuggestionIsTop(Suggestion s)
    {
        if(s.getIstop().equals(ISTOP_FALSE))
        {
            s.setIstop(ISTOP_TURE);
        }
        else if(s.getIstop().equals(ISTOP_TURE))
        {
            s.setIstop(ISTOP_FALSE);
        }
    }

    @Override
    public boolean newSuggestions(String userID, String suggestionStr) {
        Admin admin = adminMapper.selectByPrimaryKey(userID);
        if(admin==null) return false;
        Suggestion suggestion = new Suggestion();
        suggestion.setId(0);
        suggestion.setSpeakername(admin.getName());
        suggestion.setSpeakerid(admin.getId());
        suggestion.setContent(suggestionStr);
        suggestion.setDate(new Date());
        suggestion.setSpeakertype("管理员");
        suggestion.setState(STATE_PASS);
        suggestion.setIstop(ISTOP_FALSE);
        return suggestionMapper.insert(suggestion)==1;
        //判断受影响行数
    }

    @Override
    public boolean delSuggestion(Suggestion s) {
        return suggestionMapper.deleteByPrimaryKey(s.getId())==1;
    }

}
